﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-21-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
using System.Globalization;
namespace NMortgage.UI.Model
{
    using RAND = Troschuetz.Random;

    public sealed class UniformDistribution : ABaseValidatedType<UniformDistribution, UniformDistributionValidator>, IDistribution
    {
        #region Fields

        private RAND.ContinuousUniformDistribution distribution;
        private RAND.Generator generator;
        private double maximum;
        private double minimum;

        private const double DEFAULT_MIN = 2.0d;
        private const double DEFAULT_MAX = 6.9d;

        #endregion Fields

        #region Constructors

        public UniformDistribution()
        {
            Minimum = DEFAULT_MIN;
            Maximum = DEFAULT_MAX;

            generator = new RAND.StandardGenerator();
            distribution = new RAND.ContinuousUniformDistribution(generator);
        }

        #endregion Constructors

        #region Properties

        public string Description
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, Properties.Resources.UniformRateRateDescriptionFormat, Minimum, Maximum);
            }
        }

        public double Maximum
        {
            get { return maximum; }
            set
            {
                maximum = value;
                NotifyOfPropertyChange(() => Maximum);
                NotifyOfPropertyChange(() => Minimum);
            }
        }

        public double Minimum
        {
            get { return minimum; }
            set
            {
                minimum = value;
                NotifyOfPropertyChange(() => Maximum);
                NotifyOfPropertyChange(() => Minimum);
            }
        }

        #endregion Properties

        #region Methods

        public double GetRate()
        {
            return ScaleRandomToPercentRange(distribution.NextDouble());
        }

        private double ScaleRandomToPercentRange(double randomNummber)
        {
            return (randomNummber * (Maximum - Minimum) + Minimum) / 100d;
        }

        #endregion Methods
    }
}